import javax.swing.tree.TreeNode;

/**
 * Created by JiaJia on 2019/5/21 0021.
 * 二叉搜索树中的第k个结点
 * 二叉搜索树的中序遍历结果刚好是排序数列
 */
public class Code63 {
    private TreeNode res;
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;

        }

    }
    public TreeNode KthNode(TreeNode pRoot,int k){
        inOrder(pRoot,k);
        return res;
    }
    private void inOrder(TreeNode root,int k){
        int count=0;
        if(root==null||k<1){
            return;
        }
        count++;
        inOrder(root.left,k);
        if (count==k){
            res=root;
        }
        inOrder(root.right,k);
    }
}